name: CI

on:
  push:
    branches:
      - main
    paths-ignore:
      - '**.md'
      - '.github/workflows/issue_moderator.yml'

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: true

env:
  CI_CHUNK_SIZE: 65

jobs:
  build_individual:
    name: Build individual modules
    runs-on: ubuntu-latest
    steps:
      - name: Checkout main branch
        uses: actions/checkout@v4

      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          java-version: 11
          distribution: adopt

      - name: Prepare signing key
        run: |
          echo ${{ secrets.SIGNING_KEY }} | base64 -d > signingkey.jks

      - name: Build extensions (chunk ${{ matrix.chunk }})
        uses: gradle/gradle-build-action@v2
        env:
          ALIAS: ${{ secrets.ALIAS }}
          KEY_STORE_PASSWORD: ${{ secrets.KEY_STORE_PASSWORD }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
        with:
          arguments: assembleRelease

      - name: Upload APKs
        uses: actions/upload-artifact@v4
        if: "github.repository == 'tachiyomiorg/extensions'"
        with:
          name: "individual-apks"
          path: "**/*.apk"
          retention-days: 1

      - name: Clean up CI files
        run: rm signingkey.jks

  publish_repo:
    name: Publish repo
    needs:
      - build_individual
    if: "github.repository == 'tachiyomiorg/extensions'"
    runs-on: ubuntu-latest
    steps:
      - name: Download APK artifacts
        uses: actions/download-artifact@v4
        with:
          path: ~/apk-artifacts

      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: adopt

      - name: Checkout main branch
        uses: actions/checkout@v4
        with:
          ref: main
          path: main

      - name: Create repo artifacts
        run: |
          cd main
          ./.github/scripts/move-apks.sh
          INSPECTOR_LINK="$(curl -s "https://api.github.com/repos/tachiyomiorg/tachiyomi-extensions-inspector/releases/latest" | jq -r '.assets[0].browser_download_url')"
          curl -L "$INSPECTOR_LINK" -o ./Inspector.jar
          java -jar ./Inspector.jar "apk" "output.json" "tmp"
          ./.github/scripts/create-repo.sh

      - name: Checkout repo branch
        uses: actions/checkout@v4
        with:
          ref: repo
          path: repo

      - name: Deploy repo
        run: |
          cd repo
          ../main/.github/scripts/commit-repo.sh
